package com.heaven1949.tmall.common.datasource.config;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * 数据源切面配置
 *
 * @author zhangjian
 * @date 2023-02-06 10:22:26
 */
@Aspect
@Component
public class DataSourceAspect {

    @Pointcut("@annotation(com.heaven1949.tmall.common.datasource.config.DataSource)")
    public void pointCut() {
    }

    @Before("pointCut() && @annotation(dataSource)")
    public void before(DataSource dataSource) {
        DynamicDataSource.setDataSource(dataSource.value().getDbName());
    }

    @After("pointCut()")
    public void after() {
        DynamicDataSource.clearDataSource();
    }

}
